package net.lovesrc.allin.biz.dublecow;

import com.alibaba.fastjson.JSONObject;
import com.zhouzhou.fastjava.http.FilePart;
import com.zhouzhou.fastjava.http.Requests;
import net.lovesrc.allin.biz.BaseService;
import net.lovesrc.allin.biz.idcard.IdCradFromFileService;
import net.lovesrc.allin.utils.MyRandomUtils;
import net.lovesrc.support.service.mobileverifycode.MobileVerifyCodeService;
import net.lovesrc.support.service.mobileverifycode.MobileVerifyCodeWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Component
public class DoubleCowRegister extends BaseService {

    @Autowired
    private MobileVerifyCodeService noNameMobileVerifyCodeService;
    Random random = new Random();
    //18170607025----zxl123456---2F9ATG
    //18107965722----zxf123456---DEBATG
    //18107067457----zxf123456---DEHATG
    List<String> arrays = Arrays.asList("2F9ATG","DEBATG","DEHATG");

    public void newRegister() {

        String mobile;
        try {
            mobile = doGetNewMobile();
        } catch (Exception e) {
            try {
                TimeUnit.SECONDS.sleep(6);
            } catch (InterruptedException ex) {

            }
            throw e;
        }


        JSONObject jsonObject = Requests.post("https://api.niuniubtc.com/uc/mobile/code")
                .param("phone", mobile)
                .param("country", "中国")
                .param("mylang", "zh_CN")
                .execute().asJson();
        if (jsonObject == null || !"0".equals(jsonObject.getString("code"))) {
            return;
        }

        String verifyCode = getMobileVerifyCodeWrapper().getVerifyCodeFromPhone(mobile);

        String password = MyRandomUtils.randomPassword();

        int i = random.nextInt(arrays.size());
        JSONObject jsonObject1 = Requests.post("https://api.niuniubtc.com/uc/register/phone")
                .param("phone", mobile)
                .param("username", mobile)
                .param("password", password)
                .param("promotion", arrays.get(i))
                .param("code", verifyCode)
                .param("country", "中国")
                .execute().asJson();

        if (jsonObject1 == null || !"0".equals(jsonObject1.getString("code"))) {
            return;
        }





        JSONObject regResJson = Requests.post("https://api.niuniubtc.com/uc/login")
                .param("username", mobile)
                .param("password", password)
                .param("mylang", "中国")
                .execute()
                .asJson();

        if (regResJson == null || !"0".equals(regResJson.getString("code"))) {
            return;
        }

        String token = regResJson.getJSONObject("data").getString("token");

        String[] ss = IdCradFromFileService.getIdCard();

        FilePart file1 = FilePart.builder()
                .name("file")
                .filename(ss[0] + "1.jpg")
                .contentType("image/jpeg")
                .file(new File(ss[2]))
                .build();

        FilePart file2 = FilePart.builder()
                .name("file")
                .filename(ss[0] + "2.jpg")
                .contentType("image/jpeg")
                .file(new File(ss[3]))
                .build();


        JSONObject fileJson1 = Requests.post("https://api.niuniubtc.com/uc/upload/local/image")
                .header("x-auth-token", token)
                .part(file1)
                .execute()
                .asJson();


        JSONObject fileJson2 = Requests.post("https://api.niuniubtc.com/uc/upload/local/image")
                .header("x-auth-token", token)
                .part(file2)
                .execute()
                .asJson();

        Requests.post("https://api.niuniubtc.com/uc/approve/real/name")
                .header("x-auth-token", token)
                .param("realName", ss[0])
                .param("idCard", ss[1])
                .param("idCardFront", fileJson1.getString("data"))
                .param("idCardBack", fileJson2.getString("data"))
                .param("handHeldIdCard", fileJson2.getString("data"))
                .param("mylang", "zh_CN")
                .execute()
                .asJson();

                JSONObject newRow = new JSONObject()
                    .fluentPut("number", mobile)
                    .fluentPut("password", password)
                    .fluentPut("invite_code",  arrays.get(i))
                    .fluentPut("idcard",ss[1]);
            insertRow(jdbcTemplate, "t_cows_userinfo", newRow);

        return;
    }

    private String doGetNewMobile() {
        for (; ; ) {
            String mobile = getMobileVerifyCodeWrapper().getPhone(null,"2");
//            if (jdbcTemplate.queryForObject("select count(1) from MGT_USER_INFO where username = ?", long.class, mobile) > 0) {
//                getMobileVerifyCodeWrapper().addBlack(mobile);
//                continue;
//            }
            if (StringUtils.isEmpty(mobile) || mobile.startsWith("19") || mobile.startsWith("17")) {
                continue;
            }

            return mobile;
        }
    }

    private MobileVerifyCodeWrapper getMobileVerifyCodeWrapper() {
        return new MobileVerifyCodeWrapper(noNameMobileVerifyCodeService, "24956");
    }

}
